/*
 * This is a solution for excrecise 1 in the BGU Introduction to C course.
 * Please note this is bad programming. Using global variables in a program
 * and not dividing it to functions and procedures is not recommended. =(
 */

#include <stdio.h>
#include <stdlib.h>

/*
 * Using TODO keyword inside a program might be a good idea. In Unix system and in Windows 7 Power Shell
 * there is a program named "grep". This program will return all the lines in bunch of files (or one in this case)
 * with the word it receives in line. So e.g. grep *.c TODO will return all the lines having TODO in them. -> Excellent for
 * long and complicated programs.
 */

void main(){

	/*
	 * Since we are using a program all the variables are global, and therfore should be declared at the beginning,
	 * to avoid declaring the same variable twice.
	 */

	/*initializing variables for part 1*/
	int answer=1; 		/*the variable for the menu*/
	int badInput=0;		/*counter for the number of bad inputs*/

	/*initializing variables for part 2*/
	int number2;		/*the variable for the second option in the menu*/
	int trulyAscending=1;
	int trulyDescending=1;
	int ascending=1;
	int descending=1;
	int lastDigit;
	int temp;			/*temp variable to use with the program*/

	/*initializing variables for part 3*/
	int firstSquareHeight;
	int firstSquareWidth;
	int secondSquareHeight;
	int secondSquareWidth;
	int thirdSquareHeight;
	int thirdSquareWidth;

	int firstSquareMaxHeight=20;
	int firstSquareMaxWidth=70;
	int secondSquareMaxHeight=14;
	int secondSquareMaxWidth=48;
	int thirdSquareMaxHeight=4;
	int thirdSquareMaxWidth=38;

	int firstSquareMinHeight=10;
	int firstSquareMinWidth=20;
	int secondSquareMinHeight=6;
	int secondSquareMinWidth=10;
	int thirdSquareMinHeight=2;
	int thirdSquareMinWidth=6;

	int startOH, endOH, startMH, endMH, startIH, endIH;
	int startOW, endOW, startMW, endMW, startIW, endIW;

	int i,j;							/*for loop variables*/

	/*initialize variables for part 4*/

	int k,l;
	int number4;
	int notPrime;
	int eurika;

	/*Prints the menu*/
	printf("The menu :\n");
	printf("0. Exit.\n");
	printf("1. Print menu again.\n");
	printf("2. Ascending / descending number.\n");
	printf("3. Layers square printing.\n");
	printf("4. Goldbach theory.\n");
	printf("Please enter your choice : ");

	/*First Input from the user*/
	scanf("%d",&answer);

	while((answer!=0)&&(badInput<4)){ 	/*while the input is not 0 (exit) and the number of bad inputs
										 *is smaller than 5 do the following*/
		if(answer==1){
			printf("The menu :\n");
			printf("0. Exit.\n");
			printf("1. Print menu again.\n");
			printf("2. Ascending / descending number.\n");
			printf("3. Layers square printing.\n");
			printf("4. Goldbach theory.\n");
			printf("Please enter your choice : ");

			scanf("%d",&answer);
		}
		if(answer==2){
			printf("Please enter a posetive number : "); 		/*the bad spelling is by the university, and because we don't*/
			scanf("%d",&number2);								/*want to fail the diff test, we must do the same..*/

			while(number2<1){
				scanf("%d",&number2);
			}

			trulyAscending=1;
			trulyDescending=1;
			ascending=1;
			descending=1;

			temp=number2;
			lastDigit=temp%10;

			while(temp>9){

				temp=temp/10;

				if((temp%10)>lastDigit){
					ascending=0;
					trulyAscending=0;
				}else{
					if((temp%10)==lastDigit){
						trulyAscending=0;
						trulyDescending=0;
					}else{
						descending=0;
					}
				}

				lastDigit=temp%10;
			}

			if((ascending)&&(!trulyAscending)&&(!descending)){		/*ascending*/
				printf("%d is an ascending number.\n",number2);
			}
			if((ascending)&&(trulyAscending)){						/*truly ascending*/
				printf("%d is truly ascending number.\n",number2);
			}
			if((descending)&&(trulyDescending)){					/*truly descending*/
				printf("%d is truly descending number.\n",number2);
			}
			if((descending)&&(!trulyDescending)&&(!ascending)){		/*descending*/
				printf("%d is descending number.\n",number2);
			}
			if((!descending)&&(!ascending)){						/*not ascending and not descending*/
				printf("%d is not ascending and not descending number.\n",number2);
			}
			if((descending)&&(ascending)){							/*both descending and ascending*/
				printf("%d is both ascending and descending number.\n",number2);
			}

			printf("Now enter a new menu input ( enter 1 for re-printing the menu ) : ");
			scanf("%d",&answer);
		}
		if(answer==3){

			printf("Please enter outer square even width ( between %d - %d ) : ",firstSquareMinWidth,firstSquareMaxWidth);
			scanf("%d",&firstSquareWidth);
			while((firstSquareWidth<firstSquareMinWidth)||(firstSquareWidth>firstSquareMaxWidth)||(firstSquareWidth%2!=0)){
				scanf("%d",&firstSquareWidth);
			}

			printf("Please enter outer square even height ( between %d - %d ) : ",firstSquareMinHeight,firstSquareMaxHeight);
			scanf("%d",&firstSquareHeight);
			while((firstSquareHeight<firstSquareMinHeight)||(firstSquareHeight>firstSquareMaxHeight)||(firstSquareHeight%2!=0)){
				scanf("%d",&firstSquareHeight);
			}

			printf("Please enter middle square even width ( between %d - %d ) : ",secondSquareMinWidth,secondSquareMaxWidth);
			scanf("%d",&secondSquareWidth);
			while((secondSquareWidth<secondSquareMinWidth)||(secondSquareWidth>secondSquareMaxWidth)||(secondSquareWidth>firstSquareWidth)||(secondSquareHeight%2!=0)){
				scanf("%d",&secondSquareWidth);
			}

			printf("Please enter middle square even height ( between %d - %d ) : ",secondSquareMinHeight,secondSquareMaxHeight);
			scanf("%d",&secondSquareHeight);
			while((secondSquareHeight<secondSquareMinHeight)||(secondSquareHeight>secondSquareMaxHeight)||(secondSquareHeight>firstSquareHeight)||(secondSquareHeight%2!=0)){
				scanf("%d",&secondSquareHeight);
			}

			printf("Please enter inner square even width ( between %d - %d ) : ",thirdSquareMinWidth,thirdSquareMaxWidth);
			scanf("%d",&thirdSquareWidth);
			while((thirdSquareWidth<thirdSquareMinWidth)||(thirdSquareWidth>thirdSquareMaxWidth)||(thirdSquareWidth>secondSquareWidth)||(thirdSquareWidth%2!=0)){
				scanf("%d",&thirdSquareWidth);
			}

			printf("Please enter inner square even height ( between %d - %d ) : ",thirdSquareMinHeight,thirdSquareMaxHeight);
			scanf("%d",&thirdSquareHeight);
			while((thirdSquareHeight<thirdSquareMinHeight)||(thirdSquareHeight>thirdSquareMaxHeight)||(thirdSquareHeight>secondSquareHeight)||(thirdSquareHeight%2!=0)){
				scanf("%d",&thirdSquareHeight);
			}

			startOW=0;
			startMW=(firstSquareWidth-secondSquareWidth)/2;
			startIW=startMW+(secondSquareWidth-thirdSquareWidth)/2;
			endOW=firstSquareWidth;
			endMW=firstSquareWidth-(firstSquareWidth-secondSquareWidth)/2;
			endIW=secondSquareWidth+startMW-(secondSquareWidth-thirdSquareWidth)/2;
			startOH=0;
			startMH=(firstSquareHeight-secondSquareHeight)/2;
			startIH=startMH+(secondSquareHeight-thirdSquareHeight)/2;
			endOH=firstSquareHeight;
			endMH=firstSquareHeight-startMH;
			endIH=secondSquareHeight+startMH-(secondSquareHeight-thirdSquareHeight)/2;


			for(i=0;i<=firstSquareHeight;i++){
				for(j=0;j<=firstSquareWidth;j++){
					if(((i>=startIH)&&(i<=endIH))&&(j>=startIW)&&(j<=endIW)){
						if(((i+j)%2)==0){
							printf(" ");
						}else{
							printf("I");
						}
					}else{
						if((i>=startMH)&&(i<=endMH)&&(j>=startMW)&&(j<=endMW)){
							printf("M");
						}else{
							if((i>=startOH)&&(i<=endOH)&&(j>=startOW)&&(j<=endOW)){
								printf("O");
							}
						}
					}
				}
				printf("\n");
			}

			printf("Now enter a new menu input ( enter 1 for re-printing the menu ) : ");
			scanf("%d",&answer);
		}
		if(answer==4){

			eurika=0;

			printf("Please enter a posetive even integer ( greater then 2 ) : ");			/*as written before, the spelling mistakes are not mine.*/
			scanf("%d",&number4);
			while((number4%2!=0)||(number4<=2)){
				printf("Wrong input, please enter a posetive even integer ( grater then 2 ) : ");
				scanf("%d",&number4);
			}
			/*this is a rubbish way to do this, but we are limited, so... =(   */
			for(k=2;(k<(number4/2)+1)&&(!eurika);k++){								/*for loop to find the first number*/

				notPrime=0;												/*Initialize boolean variable*/

				for(l=2;l<k/2+1;l++){										/*loop to check to see if number is prime*/
					if(k%l==0){
						notPrime=1;										/*if not prime change flag*/
					}
				}

				if(!notPrime){

					notPrime=0;

					for(l=2;l<((number4-k)/2)+1;l++){
						if((number4-k)%l==0){
							notPrime=1;
						}
					}
					if(!notPrime){
						eurika=1;
					}
				}
				if(eurika){
					printf("The first primes adding : %d + %d = %d.\n",k,number4-k,number4);
				}
			}
			if(!eurika){
				printf("Goldbach was wrong! You are a genius!\n");
			}

			printf("Now enter a new menu input ( enter 1 for re-printing the menu ) : ");
			scanf("%d",&answer);
		}
		if((answer<0)||(answer>4)){
			badInput++;
			printf("Wrong input, please try again ( enter 1 for re-printing the menu ) : ");
			scanf("%d",&answer);
		}
	}
}
